import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import random

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'hello.html')
    self.response.out.write(template.render(path, template_values))

class MainHandlerTwo(webapp.RequestHandler):
  def get(self):
    name = self.request.get('name')
    self.response.out.write('Hello '+name)
    self.response.out.write("<a href = '/static/BigBrother_site.html'> <br> My BigBrother Site </a>")

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'name' : self.request.get('name') }
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class WrongAnswer:
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct


class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    addition_guess = self.request.get('addition')
    capital_guess = self.request.get('capital')
    template_values = {'name' : self.request.get('name'),
                       'wrong_answers': []}
    if addition_guess.isadigit() == true and capital_guess.isadigit() == false:
    if addition_guess !="7":
      template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))

    if capital_guess != "Sacramento":
      template_values['wrong_answers'].append(WrongAnswer("capital of California", capital_guess, "Sacramento"))

    if addition_guess != '' and capital_guess != '':
      path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
      self.response.out.write(template.render(path, template_values))
    else:
      path = os.path.join(os.path.dirname(__file__), 'quiz.html')
      self.response.out.write(template.render(path, template_values))

class DiceRollHandler(webapp.RequestHandler):
  def get(self):
    name = self.request.get('name')
    self.response.out.write('Hello '+name+'<br><br>')
    times = self.request.get('times')

    if times.isdigit():
      self.response.out.write('Roll ' + str(times) + ' times<br>')
      for roll_num in range(int(times)):
        # Simulate rolling two dice.
        dice_1 = random.randint(1, 6)
	dice_2 = random.randint(1, 6)
	self.response.out.write('<br>Rolls: '+str(dice_1) + str(dice_2))

	if dice_1 == 6 and dice_2 == 6:
	  self.response.out.write('<br>Double Sixes')
	elif dice_1 == 1 and dice_2 == 1:
	  self.response.out.write('<br>Snake Eyes')

def main():
  application = webapp.WSGIApplication([('/quizmain.*', MainHandler),
					('/hello.*', MainHandlerTwo),
                                        ('/quiz', QuizHandler),
					('/roll.*',DiceRollHandler),
                                        ('/grade_quiz', GradeQuizHandler),],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

